package com.wandian.xml;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.wandian.util.Config;
import com.wandian.util.Log;
import com.wandian.util.PathUtil;

/**
 * 网络服务器的一些xml配置
 * 
 * @author long
 * @Create 2012-10-18 下午4:12:54
 * @QQ 108232706
 * @version 1.0
 * @description NetConfig
 */
public class NetConfig {
	//房间列表类型配置
	/*public static Map<String, RoomConfigXml> roomTypeMap;//= new HashMap<String, RoomConfigXml>();

	public static List<RoomConfigItem> newHandList = new ArrayList<RoomConfigItem>();//新手
	public static List<RoomConfigItem> juniorList = new ArrayList<RoomConfigItem>();//初级
	public static List<RoomConfigItem> middleList = new ArrayList<RoomConfigItem>();//初级
	public static List<RoomConfigItem> advancedList = new ArrayList<RoomConfigItem>();//高级
	public static List<RoomConfigItem> masterList = new ArrayList<RoomConfigItem>();//大师

	public static boolean init() {
		if (!initRoomConfig()) {
			return false;
		}
		//		printList(newHandList);
		//		printList(juniorList);
		//		printList(middleList);
		//		printList(advancedList);
		//		printList(masterList);
		return true;
	}

	private static void printList(List<RoomConfigItem> newHandList2) {
		Iterator<RoomConfigItem> it = newHandList2.iterator();
		RoomConfigItem item;
		while (it.hasNext()) {
			item = it.next();
			System.out.println(item.id + " " + item.name);
		}
	}

	private static boolean initRoomConfig() {
		try {
			String roomPath = Config.getValue("room.path");
			roomPath = PathUtil.getAasolutePath(roomPath);
			SAXReader reader = new SAXReader();
			Document doc = reader.read(roomPath);
			Element root = doc.getRootElement();
			if (!initRoom(root))
				return false;
			return true;
		} catch (Exception e) {
			Log.error("init global config file fialed., server start failed.");
		}
		return false;
	}

	private static boolean initRoom(Element root) {
		if (root == null) {
			return false;
		}
		Iterator<Element> itr = root.elementIterator("room");
		Element element;
		while (itr.hasNext()) {
			element = itr.next();
			String type = element.attributeValue("type");
			if ("P1".equals(type)) {
				initRoomItem(element, newHandList);
			} else if ("G0".equals(type)) {
				initRoomItem(element, juniorList);
			} else if ("G1".equals(type)) {
				initRoomItem(element, middleList);
			} else if ("G2".equals(type)) {
				initRoomItem(element, advancedList);
			} else if ("G3".equals(type)) {
				initRoomItem(element, masterList);
			}
		}
		return true;
	}

	private static RoomConfigXml initRoomItem(Element root, List<RoomConfigItem> roomtypeList) {
		RoomConfigXml roomConfigXml = new RoomConfigXml();
		if (root == null) {
			return roomConfigXml;
		}
		Iterator<Element> itr = root.elementIterator("item");
		Element element;
		while (itr.hasNext()) {
			RoomConfigItem roomConfigItem = new RoomConfigItem();
			element = itr.next();
			roomConfigItem.id = element.attributeValue("id");
			roomConfigItem.name = element.attributeValue("name");
			roomConfigItem.begin = Integer.valueOf(element.attributeValue("begin"));
			roomConfigItem.end = Integer.valueOf(element.attributeValue("end"));
			roomConfigItem.blind = Integer.valueOf(element.attributeValue("blind"));
			roomConfigItem.min = Integer.valueOf(element.attributeValue("min"));
			roomConfigItem.max = Integer.valueOf(element.attributeValue("max"));
			roomConfigItem.seat = Integer.valueOf(element.attributeValue("seat"));
			roomConfigItem.maxplayer = Integer.valueOf(element.attributeValue("maxplayer"));
			roomConfigItem.isvip = Short.valueOf(element.attributeValue("isvip"));
			roomConfigItem.serverid = Short.valueOf(element.attributeValue("serverid"));
			roomConfigItem.bettime = Short.valueOf(element.attributeValue("bettime"));
			roomtypeList.add(roomConfigItem);
		}
		return roomConfigXml;
	}

	private static boolean initRoomConfig0() {
		try {
			String roomPath = Config.getValue("room.path");
			roomPath = PathUtil.getAasolutePath(roomPath);
			SAXReader reader = new SAXReader();
			Document doc = reader.read(roomPath);
			Element root = doc.getRootElement();
			if (!initRoom0(root))
				return false;
			return true;
		} catch (Exception e) {
			Log.error("init global config file fialed., server start failed.");
		}
		return false;
	}

	private static boolean initRoom0(Element root) {
		if (root == null) {
			return false;
		}
		Iterator<Element> itr = root.elementIterator("room");
		Element element;
		while (itr.hasNext()) {
			element = itr.next();
			String type = element.attributeValue("type");
			roomTypeMap.put(type, initRoomItem0(element));
			Log.info("parse room config succ");
		}
		return false;
	}

	private static RoomConfigXml initRoomItem0(Element root) {
		RoomConfigXml roomConfigXml = new RoomConfigXml();
		if (root == null) {
			return roomConfigXml;
		}
		Iterator<Element> itr = root.elementIterator("item");
		Element element;
		while (itr.hasNext()) {
			RoomConfigItem roomConfigItem = new RoomConfigItem();
			element = itr.next();
			roomConfigItem.id = element.attributeValue("id");
			roomConfigItem.name = element.attributeValue("name");
			roomConfigItem.begin = Integer.valueOf(element.attributeValue("begin"));
			roomConfigItem.end = Integer.valueOf(element.attributeValue("end"));
			roomConfigItem.blind = Integer.valueOf(element.attributeValue("blind"));
			roomConfigItem.min = Integer.valueOf(element.attributeValue("min"));
			roomConfigItem.max = Integer.valueOf(element.attributeValue("max"));
			roomConfigItem.seat = Integer.valueOf(element.attributeValue("seat"));
			roomConfigItem.maxplayer = Integer.valueOf(element.attributeValue("maxplayer"));
			roomConfigItem.isvip = Short.valueOf(element.attributeValue("isvip"));
			roomConfigItem.serverid = Short.valueOf(element.attributeValue("serverid"));
			roomConfigItem.bettime = Short.valueOf(element.attributeValue("bettime"));
			roomConfigXml.ItemsMap.put(roomConfigItem.id, roomConfigItem);
			Log.info("parse roomItem config succ");
		}
		return roomConfigXml;
	}*/

}
